/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core
{
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import funit.core.events.SchedulerCompleteEvent;
	import funit.core.events.SchedulerEvent;
	
	import mx.collections.ArrayCollection;
	
	[Event(name="complete", type="funit.core.events.SchedulerCompleteEvent")]
	[Event(name="schedule", type="funit.core.events.SchedulerEvent")]
	
	[ExcludeClass]
	public class TestScheduler extends EventDispatcher
	{
		private var _taskQueue:ArrayCollection;
		
		private var _isRunning:Boolean = false;
		private var _cancelationFlag:Boolean = false;
		
		
		/**
		 * @param tests
		 */
		public function TestScheduler( tests:Array = null )
		{
			_taskQueue = new ArrayCollection(tests);
		}
		
		
		/**
		 * 
		 */
		public function stopScheduling() : void
		{
			if (_isRunning)
			{
				_cancelationFlag = true;
			}
		}
		
		/**
		 * 
		 */
		public function scheduleNextTest() : void
		{
			if (_taskQueue.length > 0)
			{
				_isRunning = true;
				
				// For optimal UI vs. testing performance, a frame delay is only
				// necessary if a render has been scheduled or to minimize ths risk
				// of scripting timeouts. Until this level of scheduling is needed,
				// the schedule request will free the main thread for all cases.
				delayTestScheduling(
					ITest(_taskQueue.getItemAt(0))
				);
			}
			else
			{
				_isRunning = false;
				
				dispatchEvent(
					new SchedulerCompleteEvent(SchedulerCompleteEvent.COMPLETE)
				);
			}
		}
		
		/**
		 * @param test
		 */
		private function delayTestScheduling( test:ITest ) : void
		{
			var timer:Timer = new Timer(1, 1);
			timer.addEventListener(
				TimerEvent.TIMER_COMPLETE,
				function( event:TimerEvent ) : void
				{
					scheduleTest(test);
				}
			);
			
			timer.start();
		}
		
		/**
		 * @param test
		 */
		private function scheduleTest( test:ITest ) : void
		{
			_isRunning = false;
			
			if (_cancelationFlag)
			{
				_cancelationFlag = false;
				return;
			}
			
			removeTest(test);
			
			dispatchEvent(
				new SchedulerEvent(SchedulerEvent.SCHEDULE, test)
			);
		}
		
		/**
		 * @param test
		 */
		public function addTest( test:ITest ) : void
		{
			_taskQueue.addItem(test);
		}
		
		/**
		 * @param test
		 */
		public function removeTest( test:ITest ) : void
		{
			var index:int = _taskQueue.getItemIndex(test);
			
			if (index != -1)
			{
				_taskQueue.removeItemAt(index);
			}
		}
		
		
		/**
		 * 
		 */
		public function get isRunning() : Boolean
		{
			return _isRunning;	
		}
		
	}
	
}
